package top.bug.blog.controller.front;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;
import top.bug.blog.annotation.RateLimit;
import top.bug.blog.common.Result;
import top.bug.blog.entity.dto.article.ArticleQueryRequest;
import top.bug.blog.entity.dto.comments.CommentsAddRequest;
import top.bug.blog.entity.dto.comments.CommentsQueryRequest;
import top.bug.blog.entity.dto.common.LikeRequest;
import top.bug.blog.entity.dto.common.SingleRequest;
import top.bug.blog.entity.vo.article.ArticleCardVO;
import top.bug.blog.entity.vo.article.ArticleVO;
import top.bug.blog.entity.vo.comments.CommentsVO;
import top.bug.blog.service.ArticleService;
import top.bug.blog.service.CommentsService;
import top.bug.blog.utils.IpUtils;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

import static top.bug.blog.enums.RateLimitEnum.*;

/**
 * @author <a href="https://github.com/jiusi66">九思.</a>
 * @email 10837907@qq.com
 * @from <a href="https://www.52bug.top">递归茶馆-www.52bug.top</a>
 * @description 前台-文章模块
 */
@Api(tags = "文章管理模块")
@RestController("FrontArticleController")
@RequestMapping("/api/article")
public class ArticleController {
    @Resource
    private ArticleService articleService;
    @Resource
    private CommentsService commentsService;

    // region 文章相关

    @GetMapping("/details")
    @ApiOperation("获取文章详情")
    @RateLimit(strategyEnum = QUERY_LIMIT)
    public Result<ArticleVO> details(SingleRequest singleRequest) {
        return Result.success(articleService.details(singleRequest));
    }

    @GetMapping("/getCardInfo")
    @ApiOperation("获取作者卡片信息")
    @RateLimit(strategyEnum = QUERY_LIMIT)
    public Result<ArticleCardVO> getCardInfo(SingleRequest singleRequest, HttpServletRequest request) {
        return Result.success(articleService.getCardInfo(singleRequest, request));
    }

    @GetMapping("/page")
    @ApiOperation("分页获取文章列表")
    @RateLimit(strategyEnum = QUERY_LIMIT)
    public Result<Page<ArticleVO>> page(ArticleQueryRequest queryRequest) {
        return Result.success(articleService.getListByCid(queryRequest));
    }

    @GetMapping("/random")
    @ApiOperation("获取随机文章集合")
    @RateLimit(strategyEnum = QUERY_LIMIT)
    public Result<List<ArticleVO>> randomList() {
        return Result.success(articleService.getRandomList());
    }

    @GetMapping("/recommend")
    @ApiOperation("获取推荐文章集合")
    @RateLimit(strategyEnum = QUERY_LIMIT)
    public Result<List<ArticleVO>> recommendList() {
        return Result.success(articleService.getRecommendList());
    }

    @GetMapping("/timeline")
    @ApiOperation("获取时间轴文章数据")
    @RateLimit(strategyEnum = QUERY_LIMIT)
    public Result<List<ArticleVO>> getTimeLineList() {
        return Result.success(articleService.getTimeLineList());
    }

    // endregion

    // region 评论相关

    @GetMapping("/getComments")
    @ApiOperation("获取文章评论集合")
    @RateLimit(strategyEnum = QUERY_LIMIT)
    public Result<Page<CommentsVO>> comments(CommentsQueryRequest queryRequest) {
        return Result.success(commentsService.getCommentTreeByTarget(queryRequest));
    }

    @PostMapping("/addComment")
    @ApiOperation("发布评论")
    @RateLimit(strategyEnum = COMMENT_LIMIT)
    public Result<Void> addComment(@RequestBody CommentsAddRequest addRequest, HttpServletRequest request) {
        commentsService.addComment(addRequest, request);
        return Result.success();
    }

    // endregion

    // region 点赞相关

    @PostMapping("/like")
    @ApiOperation("内容点赞")
    @RateLimit(strategyEnum = LIKE_LIMIT)
    public Result<Boolean> likeContent(@RequestBody LikeRequest likeRequest, HttpServletRequest request) {
        String clientIp = IpUtils.getClientIp(request);
        return Result.success(articleService.tryLike(clientIp, likeRequest));
    }

    @PostMapping("/cancelLike")
    @ApiOperation("取消内容点赞")
    @RateLimit(strategyEnum = LIKE_LIMIT)
    public Result<Boolean> cancelLike(@RequestBody LikeRequest likeRequest, HttpServletRequest request) {
        String clientIp = IpUtils.getClientIp(request);
        return Result.success(articleService.cancelLike(clientIp, likeRequest));
    }

    // endregion
}